package com.example.myweather.widget;



import com.example.myweather.R;
import com.example.myweather.database.DataBaseMapper;
import com.example.myweather.database.WidgetInitData;
import com.example.myweather.modelo.Weather;
import com.example.myweather.modelo.WeatherWebService;
import com.example.myweather.modelo.WebServicesXDotNet;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.RemoteViews;

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent inten) {
		// TODO Auto-generated method stub
	
		//This code is for use the minimum of device power while widget is updating
		PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
		
		//Acquire the lock
		wl.acquire();

		
		ComponentName thiswidget = new ComponentName(context, WeatherWidgetProvider.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		int[] widgetIds = manager.getAppWidgetIds(thiswidget);
		
		DataBaseMapper dataBaseMapper = DataBaseMapper.getInstance();
		
		for(int widgetId : widgetIds){
			WidgetInitData widgetInitData = dataBaseMapper.getWidgetInitData(Integer.toString(widgetId), context);
			WeatherWidgetProvider.updateWidget(widgetInitData, context);
		}
		
		//Release the lock
		wl.release();
	}
	
	public void setOnetimeTimer(Context context){
		AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
		am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
	}

}
